<?php
class ModelRestaurantOption extends Model {	
	public function getOption($option_id) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "option` o LEFT JOIN " . DB_PREFIX . "option_description od ON (o.option_id = od.option_id) WHERE o.option_id = '" . (int)$option_id . "' AND od.language_id = '" . (int)$this->config->get('config_language_id') . "'");
		
		return $query->row;
	}
	
	public function getOptionByOptionValueId($option_value_id) {
		$query = $this->db->query("SELECT option_id FROM `" . DB_PREFIX . "option_value` WHERE option_value_id = '" . (int)$option_value_id . "'");
		
		if($query->row) {
			$option_id = $query->row['option_id'];
			
			$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "option` o LEFT JOIN " . DB_PREFIX . "option_description od ON (o.option_id = od.option_id) WHERE o.option_id = '" . (int)$option_id . "' AND od.language_id = '" . (int)$this->config->get('config_language_id') . "'");
			return $query->row;
		} else {
			return null;
		}
	}
			
	public function getOptions($data = array()) {
		$cache = md5(http_build_query($data));
		$key = 'option.' . (int)$this->config->get('config_language_id') . '.' . $cache;
		
		$option_data = $this->cache->get($key);
		if(!$option_data) {
			$sql = "SELECT DISTINCT o.option_id, o.image, o.type, o.category, od.name FROM `" . DB_PREFIX . "option` o LEFT JOIN " . DB_PREFIX . "option_description od ON (o.option_id = od.option_id)";
			
			if(isset($data['restaurant_id']) && !is_null($data['restaurant_id'])) {
				$sql .= " LEFT JOIN `" . DB_PREFIX . "restaurant_option` ro ON (o.option_id = ro.option_id)";
			}
			
			$sql .= " WHERE od.language_id = '" . (int)$this->config->get('config_language_id') . "'";
			
			if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
				$sql .= " AND LCASE(od.name) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%'";
			}
	
			$sql .= " AND `group` = '" . $this->db->escape($data['group']) . "'";
			
			if(isset($data['restaurant_id']) && !is_null($data['restaurant_id'])) {
				$sql .= " AND ro.restaurant_id = '" . (int)$data['restaurant_id'] . "'";
			}

			$sort_data = array(
				'od.name',
				'o.type',
				'o.sort_order'
			);	
			
			if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
				$sql .= " ORDER BY " . $data['sort'];	
			} else {
				$sql .= " ORDER BY od.name";	
			}
			
			if (isset($data['order']) && ($data['order'] == 'DESC')) {
				$sql .= " DESC";
			} else {
				$sql .= " ASC";
			}
			
			if (isset($data['start']) || isset($data['limit'])) {
				if ($data['start'] < 0) {
					$data['start'] = 0;
				}					
	
				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}	
			
				$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
			}	
			
			$query = $this->db->query($sql);
			
			$option_data = $query->rows;
			
			$this->cache->set($key, $option_data);
		}

		return $option_data;
	}
	
	public function getOptionDescriptions($option_id) {
		$option_data = array();
		
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "option_description WHERE option_id = '" . (int)$option_id . "'");
				
		foreach ($query->rows as $result) {
			$option_data[$result['language_id']] = array('name' => $result['name']);
		}
		
		return $option_data;
	}
	
	public function getOptionValue($option_value_id) {
		$option_value_data = null;
		
		$option_value_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "option_value ov LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (ov.option_value_id = ovd.option_value_id) WHERE ov.option_value_id = '" . (int)$option_value_id . "' AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
		
		if ($option_value_query->row) {
			$option_value = $option_value_query->row;
			
			$option_value_data = array(
				'option_value_id' => $option_value['option_value_id'],
				'name'            => $option_value['name'],
				'image'           => $option_value['image'],
				'sort_order'      => $option_value['sort_order']
			);
		}
		
		return $option_value_data;
	}
	
	public function getOptionValues($data) {
		$cache = md5(http_build_query($data));
		$key = 'option.option_value.' . (int)$this->config->get('config_language_id') . '.' . $cache;
		
		$option_value_data = $this->cache->get($key);
		if(!$option_value_data) {
			$count_restaurant_sql = "SELECT COUNT(DISTINCT ro1.restaurant_id) FROM " . DB_PREFIX . "restaurant_option ro1 INNER JOIN `" . DB_PREFIX . "option` o1 ON ro1.option_id = o1.option_id"; 
			
			if((int)$this->city->getCityId() > 0) {
				$count_restaurant_sql .= " LEFT JOIN " . DB_PREFIX . "restaurant_address ra1 ON (ro1.restaurant_id = ra1.restaurant_id)";
			}
			
			$count_restaurant_sql .= " WHERE ro1.option_value_id = ov.option_value_id";
			
			if((int)$this->city->getCityId() > 0) {
				$count_restaurant_sql .= " AND ra1.city_id = '" . (int)$this->city->getCityId() . "'";
			}
			
			$sql = "SELECT DISTINCT ov.*, ovd.*," .
						   " (" . $count_restaurant_sql . ") AS total_restaurant" .
						   " FROM (" . DB_PREFIX . "option_value ov " . 
						   " LEFT JOIN " . DB_PREFIX . "option_value_description ovd" .
						   " ON (ov.option_value_id = ovd.option_value_id))" .
						   " INNER JOIN " . DB_PREFIX . "restaurant_option ro ON ov.option_value_id = ro.option_value_id";			   
			
			$sql .= " WHERE ov.option_id = '" . (int)$data['option_id'] . "' AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "'";
			
			if(isset($data['restaurant_id'])) {
				$sql .= " AND ro.restaurant_id = '" . (int)$data['restaurant_id'] . "'";
			}
			
			$sql .= " ORDER BY ov.sort_order DESC, ovd.name ASC";
			
			$option_value_query = $this->db->query($sql);
			
			//$option_value_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "option_value ov LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (ov.option_value_id = ovd.option_value_id) WHERE ov.option_id = '" . (int)$option_id . "' AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY ov.sort_order ASC, ovd.name ASC");
					
			foreach ($option_value_query->rows as $option_value) {
				$option_value_data[] = array(
					'option_value_id' 	=> $option_value['option_value_id'],
					'name'           	=> $option_value['name'],
					'image'           	=> $option_value['image'],
					'total_restaurant'  => $option_value['total_restaurant'],
					'sort_order'      	=> $option_value['sort_order']
				);
			}
			
			$this->cache->set($key, $option_value_data);
		}
		
		return $option_value_data;
	}
	
	public function getOptionValueDescriptions($option_id) {
		$option_value_data = array();
		
		$option_value_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "option_value WHERE option_id = '" . (int)$option_id . "'");
				
		foreach ($option_value_query->rows as $option_value) {
			$option_value_description_data = array();
			
			$option_value_description_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "option_value_description WHERE option_value_id = '" . (int)$option_value['option_value_id'] . "'");			
			
			foreach ($option_value_description_query->rows as $option_value_description) {
				$option_value_description_data[$option_value_description['language_id']] = array('name' => $option_value_description['name']);
			}
			
			$option_value_data[] = array(
				'option_value_id'          => $option_value['option_value_id'],
				'option_value_description' => $option_value_description_data,
				'image'                    => $option_value['image'],
				'sort_order'               => $option_value['sort_order']
			);
		}
		
		return $option_value_data;
	}

	public function getTotalOptions($group) {
      	$query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "option` WHERE `group` = '" . $group . "'"); 
		
		return $query->row['total'];
	}		
}
?>